﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;
using PloobsEngine.Utils;
using PloobsEngine.Modelo;

namespace PloobsEngine.Physics.Bepu
{    

    /// <summary>
    /// Objeto fisico Fake, ele nao eh adicionado no mund fisico
    /// </summary>
    public class GhostObject : BepuEntityObject
    {
         /// <summary>
        /// Serialization
        /// </summary>
     public GhostObject () : base(0)
     {
     }


     public GhostObject(Vector3 position, Matrix orientation, Vector3 scale,BoundingBox box) : base(0)
     {
         this.pos = position;
         this.ori = orientation;
         this.scale = scale;
         bb = box;
     }

     bool internalMatrix = false;

        /// <summary>
        /// O Bounding BOx nao eh iniciado, favor inicia-lo manualmente, s nao ele ficara sempre no 0,0,0 e poderemos ter problema no culling
        /// </summary>
        /// <param name="position"></param>
        /// <param name="orientation"></param>
        /// <param name="scale"></param>
     public GhostObject(Vector3 position, Matrix orientation, Vector3 scale) : base(0)
         {
                    this.pos = position;
                    this.ori = orientation;
                    this.scale = scale;
         }

         private Vector3 pos;
         private Matrix ori;
         private Matrix internalWorld;
         private BoundingBox bb = new BoundingBox();

     public override Vector3 Position
     {
         get
         {
             return pos;
         }
         set
         {
             this.pos = value;
         }
     }
     public override Microsoft.Xna.Framework.Matrix Rotation
     {
         get
         {
             return ori;
         }
         set
         {
             this.ori = value;
         }
     }
     public override BoundingBox BoundingBox
     {
         get
         {
             return bb;
         }         
     }
     public override bool isMotionLess
     {
         get
         {
             return true;
         }
         set
         {
             if (value == false)
             {
                 throw new Exception("Eh sempre motion less");
             }
         }
     }
     
     public override Vector3 Velocity
     {
         get
         {
             return Vector3.Zero;
         }
         set
         {
             throw new Exception("Sempre imovel");
         }
     }

     public void setInternalWorldMatrix(Matrix mat)
     {
             internalMatrix = true;
             internalWorld = mat;
     }

     public override Matrix WorldMatrix
     {
         get
         {
             if (internalMatrix)
             {
                 return internalWorld;
             }
             else
             {
                 return Matrix.CreateScale(Scale) * Rotation * Matrix.CreateTranslation(Position);
             }
         }         

     }

        public override void DeSerialize(XmlNode node, EngineStuff engine, IModelo model, Vector3 position, Matrix rotation)
        {
            this.Position = SerializerHelper.DeserializeVector3("Position", node);
            this.Rotation = SerializerHelper.DeSerializeMatrix("Rotation", node);            
            this.Scale = SerializerHelper.DeserializeVector3("Scale", node);
            Vector3 bmax = SerializerHelper.DeserializeVector3("bbMIN", node);
            Vector3 bmmin = SerializerHelper.DeserializeVector3("bbMAX", node);
            bb = new BoundingBox(bmmin, bmax);
        }

        public override void Serialize(System.Xml.XmlTextWriter textWriter)
        {
            SerializerHelper.SerializeVector3(Scale, "Scale", textWriter);
            SerializerHelper.SerializeVector3(Position, "Position", textWriter);                        
            SerializerHelper.SerializeMatrix(Rotation, "Rotation", textWriter);
            SerializerHelper.SerializeVector3(bb.Max, "bbMAX", textWriter);
            SerializerHelper.SerializeVector3(bb.Min, "bbMIN", textWriter);            
        }

        public override PhysicObjectTypes PhysicObjectTypes
        {
            get { return PhysicObjectTypes.GHOST; }
        }    
    }
}
